package com.atguigu.common.exception;

import com.atguigu.common.result.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义异常：继承运行时异常
 */
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException{
    /**
     * 业务异常是处理业务数据校验等
     *   所以为了返回前端统一的状态，抛出异常时设置code，message,以便前端获取具体的错误信息
     */

    private Integer code;
    private String message;

    /**
     * 只传入一个消息提示的情况
     * @param message
     */
    public BusinessException(String message){
        this.message = message;
    }

    /**
     * 接收两个参数，错误code，message
     * @param code
     * @param message
     */
    public BusinessException(Integer code, String message){
        this.code = code;
        this.message = message;
    }

    /**
     *
     * @param code 错误码
     * @param message 提示消息
     * @param throwable  原始异常对象
     */
    public BusinessException(Integer code, String message, Throwable throwable){
        super(throwable);
        this.code = code;
        this.message = message;
    }

    /**
     * 接收定义好的枚举类型
     * @param responseEnum
     */
    public BusinessException(ResponseEnum responseEnum){
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

    /**
     * 接收枚举类型和原始异常对象
     * @param responseEnum
     * @param throwable
     */
    public BusinessException(ResponseEnum responseEnum, Throwable throwable){
        super(throwable);
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

}
